package com.bing.services.impl;

import com.bing.bean.GetUsersResult;
import com.bing.bean.weixin.*;
import com.bing.dao.MessageDao;
import com.bing.services.MessageService;
import com.bing.utils.Constant;
import com.bing.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2014/12/13.
 */

@Repository
@Transactional
public class MessageServiceImpl implements MessageService {
    @Autowired
    MessageDao messageDao;
    private Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);

    @Override
    public ResponseMessage findByCommand(int accountId, String command) {
        logger.info("获取{}-{}", accountId, command);
        ResponseMessage responseMessage = messageDao.findByAccountIdCommand(accountId, command);
        refactorMsg(responseMessage);
        return responseMessage;
    }

    private void refactorMsg(ResponseMessage responseMessage) {
        if (responseMessage != null && responseMessage.getImage() != null) {
            switch (responseMessage.getMsgType()) {
                case music:
                    responseMessage.setMusic(responseMessage.getImage());
                    responseMessage.setImage(null);
                    break;
                case video:
                    responseMessage.setVideo(responseMessage.getImage());
                    responseMessage.setImage(null);
                    break;
                case voice:
                    responseMessage.setVoice(responseMessage.getImage());
                    responseMessage.setImage(null);
                    break;
            }
        }
    }

    @Override
    public List<ResponseMessage> findByAccountId(int accountId) {
        List<ResponseMessage> byAccountId = messageDao.findByAccountId(accountId);
        for (ResponseMessage responseMessage : byAccountId) {
            refactorMsg(responseMessage);
        }
        return byAccountId;
    }

    @Override
    public ResultMsg createMenu(Account account, Menu menu) {
        logger.info("创建菜单Account:{}", account);
        logger.info("创建菜单Menu:{}", menu);

        String createMenuUrl = "";
        switch (account.getAccountType()) {
            case weixin:
                createMenuUrl = String.format(Constant.WEIXIN_CREATE_MENU, account.getAccessToken().getAccess_token());
                break;
            case yixin:
                createMenuUrl = String.format(Constant.YIXIN_CREATE_MENU, account.getAccessToken().getAccess_token());
                break;
        }
        return HttpUtils.post(createMenuUrl, menu.toString(), ResultMsg.class);
    }

    @Override
    public GetMenu getMenu(Account account) {

        String getMenuUrl = "";
        switch (account.getAccountType()) {
            case weixin:
                getMenuUrl = String.format(Constant.WEIXIN_GET_MENU, account.getAccessToken().getAccess_token());
                break;
            case yixin:
                getMenuUrl = String.format(Constant.YIXIN_GET_MENU, account.getAccessToken().getAccess_token());
                break;
        }
        return HttpUtils.get(getMenuUrl, GetMenu.class);
    }

    @Override
    public ResultMsg deleteMenu(Account account) {
        logger.info("删除菜单Account:{}", account);

        String deleteMenuUrl = "";
        switch (account.getAccountType()) {
            case weixin:
                deleteMenuUrl = String.format(Constant.WEIXIN_DELETE_MENU, account.getAccessToken().getAccess_token());
                break;
            case yixin:
                deleteMenuUrl = String.format(Constant.YIXIN_DELETE_MENU, account.getAccessToken().getAccess_token());
                break;
        }
        return HttpUtils.get(deleteMenuUrl, ResultMsg.class);
    }

    @Override
    public GetUsersResult getUsers(Account account, String next_openid) {
        logger.info("获取用户Account:{}", account);
        String getUsersUrl = "";
        switch (account.getAccountType()) {
            case weixin:
                getUsersUrl = String.format(Constant.WEIXIN_USER, account.getAccessToken().getAccess_token(), next_openid);
                break;
            case yixin:
                getUsersUrl = String.format(Constant.YIXIN_USER, account.getAccessToken().getAccess_token(), next_openid);
                break;
        }
        return HttpUtils.get(getUsersUrl, GetUsersResult.class);
    }
}
